package com.weaver.view;

import com.weaver.controller.Controller;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * KeyboardPanel class - Displays the virtual keyboard
 */
public class KeyboardPanel extends JPanel {
    private static final String[] KEYBOARD_ROWS = {
        "QWERTYUIOP",
        "ASDFGHJKL",
        "ZXCVBNM"
    };
    
    /**
     * Constructor
     * @param controller game controller
     */
    public KeyboardPanel(Controller controller) {
        // Set layout
        setLayout(new GridLayout(4, 1, 5, 5));
        setBorder(BorderFactory.createEmptyBorder(14, 14, 14, 14));
        
        // Create keyboard row panels
        for (String row : KEYBOARD_ROWS) {
            JPanel rowPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
            
            // Create letter buttons
            for (int i = 0; i < row.length(); i++) {
                String letter = String.valueOf(row.charAt(i));
                JButton button = createKeyButton(letter, controller);
                rowPanel.add(button);
            }
            
            add(rowPanel);
        }
        
        // Create control button row
        JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
        
        // Create backspace button
        JButton backspaceButton = createControlButton("×", "BACKSPACE", controller);
        backspaceButton.setPreferredSize(new Dimension(80, 40));
        controlPanel.add(backspaceButton);
        
        // Create enter button
        JButton enterButton = createControlButton("Enter", "ENTER", controller);
        enterButton.setPreferredSize(new Dimension(80, 40));
        controlPanel.add(enterButton);
        
        add(controlPanel);
    }
    
    /**
     * Create a letter button
     * @param letter letter
     * @param actionListener action listener
     * @return button
     */
    private JButton createKeyButton(String letter, ActionListener actionListener) {
        JButton button = new JButton(letter){
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Color bgColor = getModel().isPressed() ? Color.GRAY :
                        getModel().isRollover() ? Color.LIGHT_GRAY : new Color(98,227,251);
                g2.setColor(bgColor);
                g2.fillRoundRect(0, 0, getWidth(), getHeight(), 40, 40);
                super.paintComponent(g);
            }
        };
        button.setFont(new Font("Arial Black", Font.BOLD, 14));
        button.setFocusable(true); // Make button focusable
        button.setPreferredSize(new Dimension(50, 50));
        button.setBackground(new Color(211, 214, 218)); // Dark gray
        button.setForeground(Color.BLACK);

        button.setActionCommand("LETTER");
        button.addActionListener(e -> {
            System.out.println("Letter button clicked: " + letter); // Debug output
            actionListener.actionPerformed(e);
        });
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        return button;
    }
    
    /**
     * Create a control button
     * @param text button text
     * @param actionCommand action command
     * @param actionListener action listener
     * @return button
     */
    private JButton createControlButton(String text, String actionCommand, ActionListener actionListener) {
        JButton button = new JButton(text){
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Color bgColor = getModel().isPressed() ? Color.GRAY :
                        getModel().isRollover() ? Color.LIGHT_GRAY : new Color(98,227,251);
                g2.setColor(bgColor);
                g2.fillRoundRect(0, 0, getWidth(), getHeight(), 40, 40);
                super.paintComponent(g);
            }
        };
        button.setFont(new Font("Arial Black", Font.BOLD, 14));
        button.setFocusable(true); // Make button focusable
        button.setBackground(new Color(211, 214, 218)); // Dark gray
        button.setForeground(Color.BLACK);
        button.setActionCommand(actionCommand);
        button.addActionListener(e -> {
            System.out.println("Control button clicked: " + actionCommand); // Debug output
            actionListener.actionPerformed(e);
        });
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        return button;
    }
}